<?php

namespace App\Http\Controllers\Admin;

use App\Handlers\Tree;
use App\Http\Requests\Admin\RoleRequest;
use App\Models\Enum\Status;
use App\Models\Game;
use App\Models\Role;
use App\Repositories\Config\GamesRepository;
use App\Repositories\RulesRepository;
use App\Services\RolesService;
use App\Services\Config\GamesService;
use App\Services\Config\ChannelsService;
use Illuminate\Http\Request;
use Cache;
use Illuminate\Support\Facades\Auth;

class RolesController extends BaseController
{
    public function __construct(RolesService $rolesService, GamesService $gamesService, ChannelsService $channelsService)
    {
        $this->channelsService = $channelsService;
        $this->rolesService = $rolesService;
        $this->gamesService = $gamesService;
    }

    /**
     * 展示所有角色
     * @param Role $role
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        $reqPageSize = $request->page_size;
        $roles = $this->rolesService->getPage($request);

        return $this->view(null, compact('roles', 'reqPageSize'));
    }

    /**
     * 展示角色页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function create()
    {
        $games = $this->gamesService->getGames(['status' => Status::NORMAL]);
        $channelIds = $this->channelsService->getAll();

        return $this->view(null, compact('channelIds', 'games'));
    }

    /**
     * 添加角色
     * @param RoleRequest $request
     * @param Role $role
     * @return \Illuminate\Http\RedirectResponse
     */
    public function store(RoleRequest $request, Role $role)
    {
        $role->fill($request->all());

        $role->save();

        flash('添加角色成功')->success()->important();

        return $this->redirect($request);
    }


    /**
     * @param Role $role
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(Role $role, GamesRepository $gamesRepository)
    {
        $games = $this->gamesService->getGames();
        $channelIds = $this->channelsService->getAll();

        return $this->view('edit', compact('role', 'channelIds', 'games'));
    }

    /**
     * @param RoleRequest $request
     * @param Role $role
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(RoleRequest $request, Role $role)
    {
        $role->games()->sync($request->get('game_id'));
        $role->channels()->sync($request->get('channel_id'));

        Cache::tags('games')->flush();
        Cache::tags('channels')->flush();

        $role->update($request->all());

        flash('修改成功')->success()->important();

        return $this->redirect($request);
    }

    /**
     * @param Role $role
     * @return \Illuminate\Http\RedirectResponse
     */
    public function destroy(Role $role)
    {
        $role->rules()->detach(); //删除关联数据

        $role->delete();

        flash('删除成功')->success()->important();

        return redirect()->back();
    }

    /**
     * 展示分配权限页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function access(Role $role, RulesRepository $rulesRepository, Tree $tree)
    {
        $rules = $rulesRepository->getRules();

        $datas = $tree::channelLevel($rules, 0, '&nbsp;', 'id', 'parent_id');

        $rules = $role->rules->pluck('id')->toArray();

        return $this->view(null, compact('role', 'datas', 'rules'));
    }

    /**
     * @param Request $request
     * @param Role $role
     * @return \Illuminate\Http\RedirectResponse
     */
    public function groupAccess(Request $request, Role $role)
    {

        $role->rules()->sync($request->get('rule_id'));

        Cache::tags('rbac')->flush();

        flash('授权成功')->success()->important();

        return $this->redirect($request);
    }
}
